<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Denzel - 几个有用的Web API——桌面通知API</title>
    <meta name="description" content="前端笔记">
    <meta name="keywords" content="web前端,HTML5,CSS3,Javascript,SVG,canvas">
  <meta name="anthor" content="Denzel">
  <meta name="theme-color" content="#3eaf7c">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
  <meta name="msapplication-TileColor" content="#000000">
  <link rel="icon" href="/blog/vuepress/logo.png">
  <link rel="manifest" href="/blog/vuepress/manifest.json">
  <link rel="apple-touch-icon" href="/blog/vuepress/icons/apple-touch-icon-152x152.png">
  <link rel="mask-icon" href="/blog/vuepress/icons/safari-pinned-tab.svg" color="#3eaf7c">
    
    <link rel="preload" href="/blog/vuepress/assets/css/0.styles.d4f29607.css" as="style"><link rel="preload" href="/blog/vuepress/assets/js/app.15770e34.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/23.20d8e817.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/55.fea285a9.js" as="script"><link rel="prefetch" href="/blog/vuepress/assets/js/46.59c0ece7.js"><link rel="prefetch" href="/blog/vuepress/assets/js/1.05e4270f.js"><link rel="prefetch" href="/blog/vuepress/assets/js/2.ace739f7.js"><link rel="prefetch" href="/blog/vuepress/assets/js/3.636e5a79.js"><link rel="prefetch" href="/blog/vuepress/assets/js/4.7a55aa4b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/5.b56b0573.js"><link rel="prefetch" href="/blog/vuepress/assets/js/6.e9a3fbc8.js"><link rel="prefetch" href="/blog/vuepress/assets/js/7.aa46c47d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/8.a9288ac6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/9.b9f8dec6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/10.3e2ea470.js"><link rel="prefetch" href="/blog/vuepress/assets/js/11.4d115f98.js"><link rel="prefetch" href="/blog/vuepress/assets/js/12.c97cf4d0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/13.1e0cf594.js"><link rel="prefetch" href="/blog/vuepress/assets/js/14.bab9026a.js"><link rel="prefetch" href="/blog/vuepress/assets/js/15.1a24cffe.js"><link rel="prefetch" href="/blog/vuepress/assets/js/16.e9995850.js"><link rel="prefetch" href="/blog/vuepress/assets/js/17.86a1f7d2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/18.c32400c4.js"><link rel="prefetch" href="/blog/vuepress/assets/js/19.9f99887c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/20.82f0c2f9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/21.7f5c223d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/22.acf9934c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/24.acf27379.js"><link rel="prefetch" href="/blog/vuepress/assets/js/25.20165510.js"><link rel="prefetch" href="/blog/vuepress/assets/js/26.83a2ddc3.js"><link rel="prefetch" href="/blog/vuepress/assets/js/27.33276a04.js"><link rel="prefetch" href="/blog/vuepress/assets/js/28.e320efab.js"><link rel="prefetch" href="/blog/vuepress/assets/js/29.3fd92c30.js"><link rel="prefetch" href="/blog/vuepress/assets/js/30.2bc84d43.js"><link rel="prefetch" href="/blog/vuepress/assets/js/31.d70ed500.js"><link rel="prefetch" href="/blog/vuepress/assets/js/32.e7a0f8e1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/33.c48ee70b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/34.8ad03d1b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/35.5bdb3994.js"><link rel="prefetch" href="/blog/vuepress/assets/js/36.a25f926e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/37.7515cfbb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/38.99347371.js"><link rel="prefetch" href="/blog/vuepress/assets/js/39.3481fc62.js"><link rel="prefetch" href="/blog/vuepress/assets/js/40.bc709734.js"><link rel="prefetch" href="/blog/vuepress/assets/js/41.e1e0a3bf.js"><link rel="prefetch" href="/blog/vuepress/assets/js/42.762b61b0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/43.8f0b2d6c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/44.2a0a4ee0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/45.baf7ed60.js"><link rel="prefetch" href="/blog/vuepress/assets/js/47.11a8720a.js"><link rel="prefetch" href="/blog/vuepress/assets/js/48.e0bf8b54.js"><link rel="prefetch" href="/blog/vuepress/assets/js/49.c96c4011.js"><link rel="prefetch" href="/blog/vuepress/assets/js/50.31189263.js"><link rel="prefetch" href="/blog/vuepress/assets/js/51.766a8294.js"><link rel="prefetch" href="/blog/vuepress/assets/js/52.26b971fd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/53.0cfbd708.js"><link rel="prefetch" href="/blog/vuepress/assets/js/54.bdc15c87.js"><link rel="prefetch" href="/blog/vuepress/assets/js/56.e030a6b2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/57.bd168a1e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/58.636557fa.js"><link rel="prefetch" href="/blog/vuepress/assets/js/59.5d1d0ce6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/60.0b779f92.js"><link rel="prefetch" href="/blog/vuepress/assets/js/61.4ec3f2eb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/62.8f70f114.js"><link rel="prefetch" href="/blog/vuepress/assets/js/63.9ac2b61d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/64.c028fad5.js"><link rel="prefetch" href="/blog/vuepress/assets/js/65.923a8d15.js"><link rel="prefetch" href="/blog/vuepress/assets/js/66.73db652e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/67.1a91446d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/68.e5a12a42.js"><link rel="prefetch" href="/blog/vuepress/assets/js/69.6ddcb882.js"><link rel="prefetch" href="/blog/vuepress/assets/js/70.ceb70d85.js"><link rel="prefetch" href="/blog/vuepress/assets/js/71.a804ef40.js"><link rel="prefetch" href="/blog/vuepress/assets/js/72.f1c4098c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/73.2f28b842.js"><link rel="prefetch" href="/blog/vuepress/assets/js/74.571c2ab1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/75.f365c2a1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/76.0916a3cd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/77.469aed17.js"><link rel="prefetch" href="/blog/vuepress/assets/js/78.02cdd59d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/79.44d7f95c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/80.4ca072f1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/81.d9239a49.js"><link rel="prefetch" href="/blog/vuepress/assets/js/82.b09096b2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/83.f4bc8a9b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/84.1ffe6969.js"><link rel="prefetch" href="/blog/vuepress/assets/js/85.44e980f9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/86.c8579ac0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/87.cd49f324.js"><link rel="prefetch" href="/blog/vuepress/assets/js/88.3851b6ff.js"><link rel="prefetch" href="/blog/vuepress/assets/js/89.b05297be.js"><link rel="prefetch" href="/blog/vuepress/assets/js/90.c1e3263e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/91.b493a1bb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/92.80ab06fd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/93.c6ca00b3.js"><link rel="prefetch" href="/blog/vuepress/assets/js/94.e993d310.js"><link rel="prefetch" href="/blog/vuepress/assets/js/95.8e19a7ed.js">
    <link rel="stylesheet" href="/blog/vuepress/assets/css/0.styles.d4f29607.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div><a href="/blog/vuepress/" class="home-link router-link-active"><img src="/blog/vuepress/hero.png" class="logo"><span class="site-name can-hide">
      Denzel
    </span></a><div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""><!----></div><nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/vuepress/js/" class="nav-link router-link-active">JS</a></div><div class="nav-item"><a href="/blog/vuepress/css/" class="nav-link">CSS</a></div><div class="nav-item"><a href="/blog/vuepress/html/" class="nav-link">HTML</a></div><div class="nav-item"><a href="/blog/vuepress/else/" class="nav-link">其他</a></div><div class="nav-item"><a href="/blog/vuepress/study/" class="nav-link">study</a></div><a href="https://github.com/xiaotianxia" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header><div class="sidebar-mask"></div><div class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/blog/vuepress/js/" class="nav-link router-link-active">JS</a></div><div class="nav-item"><a href="/blog/vuepress/css/" class="nav-link">CSS</a></div><div class="nav-item"><a href="/blog/vuepress/html/" class="nav-link">HTML</a></div><div class="nav-item"><a href="/blog/vuepress/else/" class="nav-link">其他</a></div><div class="nav-item"><a href="/blog/vuepress/study/" class="nav-link">study</a></div><a href="https://github.com/xiaotianxia" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav><ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>JS</span><!----></p><ul class="sidebar-group-items"><li><a href="/blog/vuepress/js/" class="sidebar-link">目录</a></li><li><a href="/blog/vuepress/js/four_kinds_of_observers.html" class="sidebar-link">JS中的观察者们 —— 四种 Observers</a></li><li><a href="/blog/vuepress/js/editable_dom.html" class="sidebar-link">可编辑的页面——designMode、contenteditable和user-modify</a></li><li><a href="/blog/vuepress/js/useful_webapis_mediarecorder.html" class="sidebar-link">用浏览器轻松录制音频、视频—— MediaRecorder API</a></li><li><a href="/blog/vuepress/js/useful_webapis_audiocontext.html" class="sidebar-link">让你的网页开口说话 —— audioContext API</a></li><li><a href="/blog/vuepress/js/useful_webapis_fullscreen.html" class="sidebar-link">几个有用的Web API——全屏模式</a></li><li><a href="/blog/vuepress/js/useful_webapis_notification.html" class="active sidebar-link">几个有用的Web API——桌面通知API</a></li><li><a href="/blog/vuepress/js/useful_webapis_connection.html" class="sidebar-link">几个有用的Web API——网络状态API</a></li><li><a href="/blog/vuepress/js/useful_webapis_geolocation.html" class="sidebar-link">几个有用的Web API——地理位置API</a></li><li><a href="/blog/vuepress/js/speech_in_js_recognition.html" class="sidebar-link">JS中的语音识别——Speech Recognition API</a></li><li><a href="/blog/vuepress/js/speech_in_js_synthesis.html" class="sidebar-link">JS中的语音合成——Speech Synthesis API</a></li><li><a href="/blog/vuepress/js/camera_in_js_trial.html" class="sidebar-link">JS控制设备摄像头初探</a></li><li><a href="/blog/vuepress/js/shape_detection_api_trial.html" class="sidebar-link">浏览器的图像识别API初探</a></li><li><a href="/blog/vuepress/js/simple_chatroom_using_socketio.html" class="sidebar-link">socket.io实现简单群聊</a></li><li><a href="/blog/vuepress/js/communication_using_postmessage.html" class="sidebar-link">postMessage实现父子页面简单通信</a></li><li><a href="/blog/vuepress/js/upload_using_filereader_createobjecturl.html" class="sidebar-link">FileReader与URL.createObjectURL实现图片、视频上传预览</a></li><li><a href="/blog/vuepress/js/simple_code_preview.html" class="sidebar-link">前端几行代码简单实现w3school代码预览</a></li></ul></div></li></ul></div><div class="page"><div class="content"><h1 id="几个有用的web-api——桌面通知api"><a href="#几个有用的web-api——桌面通知api" aria-hidden="true" class="header-anchor">#</a> 几个有用的Web API——桌面通知API</h1><p><a href="https://denzel.netlify.com/js/useful_webapis_notification.html?_=65432123456523" target="_blank" rel="noopener noreferrer">原文链接</a></p><h2 id="简介"><a href="#简介" aria-hidden="true" class="header-anchor">#</a> 简介</h2><h3 id="构造方法"><a href="#构造方法" aria-hidden="true" class="header-anchor">#</a> 构造方法</h3><pre class="language-js"><code><span class="token keyword">let</span> notification <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Notification</span><span class="token punctuation">(</span>title<span class="token punctuation">,</span> options<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><h3 id="参数"><a href="#参数" aria-hidden="true" class="header-anchor">#</a> 参数</h3><h4 id="title"><a href="#title" aria-hidden="true" class="header-anchor">#</a> title</h4><p>通知标题</p><h4 id="options-可选"><a href="#options-可选" aria-hidden="true" class="header-anchor">#</a> options (可选)</h4><p>一个被允许用来设置通知的对象。它包含以下属性：</p><pre class="language-js"><code><span class="token operator">-</span> dir <span class="token punctuation">:</span> 文字的方向；它的值可以是 auto（自动）<span class="token punctuation">,</span> ltr（从左到右）<span class="token punctuation">,</span> or rtl（从右到左）
<span class="token operator">-</span> lang<span class="token punctuation">:</span> 指定通知中所使用的语言。
<span class="token operator">-</span> badge<span class="token punctuation">:</span> 一个图片的url，当桌面没有空间来展示该通知时，用来表示该通知的图标。
<span class="token operator">-</span> body<span class="token punctuation">:</span> 通知中额外显示的字符串。
<span class="token operator">-</span> tag<span class="token punctuation">:</span> 赋予通知一个id，以便在必要的时候对通知进行刷新、替换或移除。
<span class="token operator">-</span> icon<span class="token punctuation">:</span> 一个图片的url，将被用于显示通知的图标。
<span class="token operator">-</span> image<span class="token punctuation">:</span> 一个图片的url，显示在通知文字下方。
<span class="token operator">-</span> data<span class="token punctuation">:</span> 与通知绑定的数据，可以是任意数据类型。
<span class="token operator">-</span> vibrate<span class="token punctuation">:</span> 通知时设备震动，数组，例如<span class="token punctuation">[</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">]</span>表示设备震动<span class="token number">200</span>毫秒，然后停止<span class="token number">100</span>毫秒，再震动<span class="token number">200</span>毫秒。
<span class="token operator">-</span> renotify<span class="token punctuation">:</span> 是否重新通知，默认<span class="token boolean">false</span>。
<span class="token operator">-</span> requireInteraction<span class="token punctuation">:</span> 是否持续显示通知，<span class="token boolean">true</span>表示通知会一直显示而不会自动消失，直到用户点击或者关闭。
<span class="token operator">-</span> actions<span class="token punctuation">:</span> <span class="token function">数组，表示通知来时用户可操作的行为。</span><span class="token punctuation">(</span>不太懂怎么用<span class="token punctuation">:</span>joy<span class="token punctuation">:</span><span class="token punctuation">)</span>

<span class="token comment">// 以下是新添加的属性，目前大多数浏览器尚未支持。</span>

<span class="token operator">-</span> silent<span class="token punctuation">:</span> 是否静音，默认为<span class="token boolean">false</span>。
<span class="token operator">-</span> sound<span class="token punctuation">:</span> 声音文件的url，通知来时播放。
<span class="token operator">-</span> noscreen<span class="token punctuation">:</span> 是否不在屏幕上显示通知信息，默认为<span class="token boolean">false</span>，表示通知来时激活屏幕。
<span class="token operator">-</span> sticky<span class="token punctuation">:</span> 通知是否“粘屏”？默认<span class="token boolean">false</span>。

</code></pre><h3 id="静态属性"><a href="#静态属性" aria-hidden="true" class="header-anchor">#</a> 静态属性</h3><p><strong>Notification.permission</strong></p><p>一个用于表明当前通知显示授权状态的字符串。可能的值包括：denied (用户拒绝了通知的显示),
granted (用户允许了通知的显示), 或 default (因为不知道用户的选择，所以浏览器的行为与 denied 时相同)。</p><h3 id="实例属性"><a href="#实例属性" aria-hidden="true" class="header-anchor">#</a> 实例属性</h3><p>这些属性都是Notification实例中的属性，且都是只读的。</p><pre class="language-js"><code><span class="token operator">-</span> title  在构造方法中指定的 title 参数。
<span class="token operator">-</span> dir  通知的文本显示方向。在构造方法的 options 中指定。
<span class="token operator">-</span> lang  通知的语言。在构造方法的 options 中指定。
<span class="token operator">-</span> body  通知的文本内容。在构造方法的 options 中指定。
<span class="token operator">-</span> tag  通知的id。在构造方法的 options 中指定。
<span class="token operator">-</span> icon  通知的图标图片的url地址。在构造方法的 options 中指定。
</code></pre><h3 id="事件"><a href="#事件" aria-hidden="true" class="header-anchor">#</a> 事件</h3><pre class="language-js"><code><span class="token operator">-</span> onclick 每当用户点击通知时被触发。
<span class="token operator">-</span> onshow 当通知显示的时候被触发。
<span class="token operator">-</span> onerror 每当通知遇到错误时被触发。
<span class="token operator">-</span> onclose 当用户关闭通知时被触发。
</code></pre><h3 id="静态方法"><a href="#静态方法" aria-hidden="true" class="header-anchor">#</a> 静态方法</h3><p><strong>Notification.requestPermission()</strong></p><p>用于当前页面想用户申请显示通知的权限。这个方法只能被用户行为调用（比如：onclick 事件），并且不能被其他的方式调用。</p><h2 id="demo"><a href="#demo" aria-hidden="true" class="header-anchor">#</a> Demo</h2><h3 id="部分代码"><a href="#部分代码" aria-hidden="true" class="header-anchor">#</a> 部分代码</h3><pre class="language-js"><code><span class="token function">data</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">{</span>
        queryParams<span class="token punctuation">:</span> <span class="token punctuation">{</span>
            title<span class="token punctuation">:</span> <span class="token string">'一条新通知'</span><span class="token punctuation">,</span>
            body<span class="token punctuation">:</span> <span class="token string">'12345哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈12345'</span><span class="token punctuation">,</span>
            icon<span class="token punctuation">:</span> <span class="token string">'https://denzel.netlify.com/hero.png'</span><span class="token punctuation">,</span>
            badge<span class="token punctuation">:</span> <span class="token string">'https://denzel.netlify.com/hero.png'</span><span class="token punctuation">,</span>
            image<span class="token punctuation">:</span> <span class="token string">'https://denzel.netlify.com/hero.png'</span><span class="token punctuation">,</span>
            <span class="token comment">// silent: true,</span>
            sound<span class="token punctuation">:</span> <span class="token string">'http://pn4meizzc.bkt.clouddn.com/blogsmile.wav'</span><span class="token punctuation">,</span>
            dir<span class="token punctuation">:</span> <span class="token string">'rtl'</span><span class="token punctuation">,</span>
            data<span class="token punctuation">:</span> <span class="token punctuation">{</span>
                t<span class="token punctuation">:</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            vibrate<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        time<span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span>
        msg<span class="token punctuation">:</span> <span class="token string">''</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token function">onConfirm</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token punctuation">(</span><span class="token string">'Notification'</span> <span class="token keyword">in</span> window<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">'您的浏览器不支持通知API'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">this</span><span class="token punctuation">.</span>msg <span class="token operator">=</span> Notification<span class="token punctuation">.</span>permission<span class="token punctuation">;</span>
        <span class="token comment">//检查用户是否同意接受通知</span>
        <span class="token comment">//用户已同意</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>Notification<span class="token punctuation">.</span>permission <span class="token operator">==</span> <span class="token string">&quot;granted&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">newNotification</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//若没拒绝获取权限</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>Notification<span class="token punctuation">.</span>permission <span class="token operator">!=</span> <span class="token string">&quot;denied&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">//向用户获取权限</span>
            Notification<span class="token punctuation">.</span><span class="token function">requestPermission</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span>permission<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">newNotification</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>

    <span class="token function">newNotification</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> notification <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Notification</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>queryParams<span class="token punctuation">.</span>title<span class="token punctuation">,</span> <span class="token punctuation">{</span>
            <span class="token operator">...</span><span class="token keyword">this</span><span class="token punctuation">.</span>queryParams
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>notification<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//事件绑定</span>
        notification<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'show'</span><span class="token punctuation">,</span> e <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>$message<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">'通知出现'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>time <span class="token operator">=</span> notification<span class="token punctuation">.</span>data<span class="token punctuation">.</span>t<span class="token punctuation">.</span><span class="token function">toLocaleTimeString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


        notification<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'click'</span><span class="token punctuation">,</span> e <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>$message<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">'你点击了通知'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// notification.close();</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        notification<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'close'</span><span class="token punctuation">,</span> e <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>$message<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">'你关闭了通知'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><h3 id="demo-2"><a href="#demo-2" aria-hidden="true" class="header-anchor">#</a> demo</h3><p>有些参数暂未支持</p><div class="notification-wrapper" data-v-7fb75335><form class="el-form el-form--label-left" data-v-7fb75335><div class="el-form-item" data-v-7fb75335><label class="el-form-item__label" style="width:90px;">title:</label><div class="el-form-item__content" style="margin-left:90px;"><div class="el-input el-input--small" data-v-7fb75335><!----><input type="text" autocomplete="off" placeholder="题目..." value="一条新通知" class="el-input__inner"><!----><!----><!----></div><!----></div></div><div class="el-form-item" data-v-7fb75335><label class="el-form-item__label" style="width:90px;">body:</label><div class="el-form-item__content" style="margin-left:90px;"><div class="el-textarea" data-v-7fb75335><textarea placeholder="内容..." class="el-textarea__inner">12345哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈12345</textarea></div><!----></div></div><div class="el-form-item" data-v-7fb75335><label class="el-form-item__label" style="width:90px;">icon:</label><div class="el-form-item__content" style="margin-left:90px;"><div class="el-input el-input--small" data-v-7fb75335><!----><input type="text" autocomplete="off" placeholder="图标" value="https://denzel.netlify.com/hero.png" class="el-input__inner"><!----><!----><!----></div><!----></div></div><div class="el-form-item" data-v-7fb75335><label class="el-form-item__label" style="width:90px;">icon预览:</label><div class="el-form-item__content" style="margin-left:90px;"><img src="https://denzel.netlify.com/hero.png" alt="" data-v-7fb75335><!----></div></div><div class="el-form-item" data-v-7fb75335><label class="el-form-item__label" style="width:90px;">image:</label><div class="el-form-item__content" style="margin-left:90px;"><div class="el-input el-input--small" data-v-7fb75335><!----><input type="text" autocomplete="off" placeholder="图片" value="https://denzel.netlify.com/hero.png" class="el-input__inner"><!----><!----><!----></div><!----></div></div><div class="el-form-item" data-v-7fb75335><label class="el-form-item__label" style="width:90px;">image预览:</label><div class="el-form-item__content" style="margin-left:90px;"><img src="https://denzel.netlify.com/hero.png" alt="" data-v-7fb75335><!----></div></div><div class="el-form-item" data-v-7fb75335><label class="el-form-item__label" style="width:90px;">sound:</label><div class="el-form-item__content" style="margin-left:90px;"><div class="el-input el-input--small" data-v-7fb75335><!----><input type="text" autocomplete="off" placeholder="音频" value="http://pn4meizzc.bkt.clouddn.com/blogsmile.wav" class="el-input__inner"><!----><!----><!----></div><!----></div></div><div class="el-form-item" data-v-7fb75335><label class="el-form-item__label" style="width:90px;">sound预览:</label><div class="el-form-item__content" style="margin-left:90px;"><audio src="http://pn4meizzc.bkt.clouddn.com/blogsmile.wav" controls="controls" data-v-7fb75335></audio><!----></div></div><div class="el-form-item" data-v-7fb75335><!----><div class="el-form-item__content" style="margin-left:90px;"><button type="button" class="el-button el-button--primary el-button--small" data-v-7fb75335><!----><!----><span>通知一下~</span></button><span class="txt-red" data-v-7fb75335> </span><!----></div></div><div class="el-form-item" data-v-7fb75335><!----><div class="el-form-item__content" style="margin-left:90px;"><span data-v-7fb75335>携带data:</span><!----></div></div></form></div><h2 id="样式"><a href="#样式" aria-hidden="true" class="header-anchor">#</a> 样式</h2><p>主要看了mac下的chrome、chromium和Firefox。windows下的还请读者自行测试...</p><p>chromium应该算是支持的最好的了</p><p><img src="http://pn4meizzc.bkt.clouddn.com/blog/notification_chromium.png" alt="small-demo chromium"></p><p>chrome67和Firefox样式基本一样</p><p>chrome67</p><p><img src="http://pn4meizzc.bkt.clouddn.com/blog/notification%E2%80%94%E2%80%94chrome67.png" alt="small-demo chrome"></p><p>Firefox</p><p><img src="http://pn4meizzc.bkt.clouddn.com/blog/natification_firefox.png" alt="small-demo Firefox"></p><h2 id="兼容性"><a href="#兼容性" aria-hidden="true" class="header-anchor">#</a> 兼容性</h2><p>PC端的表现见上图，手机端嘛...chrome、Firefox等全跪😂 ，反而UC浏览器貌似支持这个属性，但是我的UC显示是denied，
不知道是我误操作拒绝了权限还是浏览器的bug，算了不深究了...</p><h2 id="总结"><a href="#总结" aria-hidden="true" class="header-anchor">#</a> 总结</h2><p>没啥好总结的，兼容性也就这样，算是为未来做点只是储备吧，我们依然坚定地——相信未来。</p><h2 id="参考资料"><a href="#参考资料" aria-hidden="true" class="header-anchor">#</a> 参考资料</h2><ul><li><a href="https://developer.mozilla.org/zh-CN/docs/Web/API/notification" target="_blank" rel="noopener noreferrer">notification</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/notification/Notification" target="_blank" rel="noopener noreferrer">Notification.Notification()</a></li><li><a href="http://www.zhangxinxu.com/wordpress/2016/07/know-html5-web-notification/" target="_blank" rel="noopener noreferrer">简单了解HTML5中的Web Notification桌面通知</a></li></ul></div><div class="wx-wrapper" data-v-0de8a4bf><p class="wx-txt" data-v-0de8a4bf>欢迎关注</p><img src="http://pn4meizzc.bkt.clouddn.com/wxqr.png" class="wx-img" data-v-0de8a4bf></div><div class="beg-wrapper" data-v-1f1af99c><a href="javascript:;" class="btn show" data-v-1f1af99c><img src="http://pn4meizzc.bkt.clouddn.com/blogshang_log.png" data-v-1f1af99c></a><p class="shang-txt" data-v-1f1af99c>赏不赏，看您心情</p><img src="http://pn4meizzc.bkt.clouddn.com/blogWechatIMG35.jpeg" class="shangImg" data-v-1f1af99c></div><!----><div class="content page-nav"><p class="inner"><span class="prev">
        ← <a href="/blog/vuepress/js/useful_webapis_fullscreen.html" class="prev">
          几个有用的Web API——全屏模式
        </a></span><span class="next"><a href="/blog/vuepress/js/useful_webapis_connection.html">
          几个有用的Web API——网络状态API
        </a> →
      </span></p></div></div></div></div>
    <script src="/blog/vuepress/assets/js/23.20d8e817.js" defer></script><script src="/blog/vuepress/assets/js/55.fea285a9.js" defer></script><script src="/blog/vuepress/assets/js/app.15770e34.js" defer></script>
  </body>
</html>
